package com.gcph.zhuoba.main.activitys;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.Fragment.CircleFragment;
import com.gcph.zhuoba.main.Fragment.HelperFragment;
import com.gcph.zhuoba.main.Fragment.MessageFragment;
import com.gcph.zhuoba.main.Fragment.NearbyFragment;
import com.gcph.zhuoba.main.Fragment.PersonalFragment;
import com.gcph.zhuoba.util.L;

/**
 * 项目的主Activity，所有的Fragment都嵌入在这里。
 * @author zhanghejie
 * 下面的五个tab分别是台球圈、附近、发布、小Q、个人中心
 */
public class MainActivity extends Activity implements OnClickListener {
	//private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
	private Fragment circleFragment;
	
	//小Q助手
	//private HelperFragment helperFragment;

	//个人中心
	//private PersonalFragment personalFragment;

	//台球圈布局
	private View circleLayout;

	//附近布局
	private View nearbyLayout;

	//小Q助手布局
	private View helperLayout;

	//个人中心布局
	private View personalLayout;
	
	/**
	 * 发布
	 */
	private View xiaoxiLayout;

	//在Tab布局上显示台球圈图标的控件
	private ImageView circleImage;

	//在Tab布局上显示附近图标的控件/
	private ImageView nearbyImage;

	//小Q助手
	private ImageView helperImage;

	//个人中心
	private ImageView personalImage;
	
	//消息 
	private ImageView xiaoxiImage;

	private TextView circleText;
	private TextView nearbyText;
	private TextView helperText;
	private TextView personalText;
	private TextView xiaoxiText;

	/**
	 * 用于对Fragment进行管理
	 */
	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 初始化布局元素
		initViews();
		fragmentManager = getFragmentManager();
		// 第一次启动时选中第0个tab
		setTabSelection(0);
	}

	/**
	 * 在这里获取到每个需要用到的控件的实例，并给它们设置好必要的点击事件。
	 */
	private void initViews() {
		circleLayout = findViewById(R.id.layout_circle);
		nearbyLayout = findViewById(R.id.contacts_layout);
		helperLayout = findViewById(R.id.news_layout);
		personalLayout = findViewById(R.id.setting_layout);
		xiaoxiLayout = findViewById(R.id.xiaoxi_layout);
		
		circleImage = (ImageView) findViewById(R.id.message_image);
		nearbyImage = (ImageView) findViewById(R.id.contacts_image);
		helperImage = (ImageView) findViewById(R.id.news_image);
		personalImage = (ImageView) findViewById(R.id.setting_image);
		xiaoxiImage = (ImageView) findViewById(R.id.xiaoxi_image);
		
		circleText = (TextView) findViewById(R.id.message_text);
		nearbyText = (TextView) findViewById(R.id.contacts_text);
		helperText = (TextView) findViewById(R.id.news_text);
		personalText = (TextView) findViewById(R.id.setting_text);
		xiaoxiText = (TextView) findViewById(R.id.xiaoxi_text);
		
		circleLayout.setOnClickListener(this);
		nearbyLayout.setOnClickListener(this);
		helperLayout.setOnClickListener(this);
		personalLayout.setOnClickListener(this);
		xiaoxiLayout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_circle:
			// 当点击了消息tab时，选中第1个tab Seek help
			setTabSelection(0);
			break;
		case R.id.contacts_layout:
			// 当点击了联系人tab时，选中第2个tab
			setTabSelection(1);
			break;
		case R.id.news_layout:
			// 当点击了动态tab时，选中第3个tab
			setTabSelection(2);
			break;
		case R.id.setting_layout:
			// 当点击了设置tab时，选中第4个tab
			setTabSelection(3);
			break;
		case R.id.xiaoxi_layout:
			setTabSelection(4);
			break;
		default:
			break;
		}
	}

	/**
	 * 根据传入的index参数来设置选中的tab页。
	 * 
	 * @param index
	 *            每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
	 */
	private void setTabSelection(int index) {
		// 每次选中之前先清楚掉上次的选中状态
		clearSelection();
		// 开启一个Fragment事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
		hideFragments(transaction);
		switch (index) {
		case 0:
			// 当点击了消息tab时，改变控件的图片和文字颜色
			circleImage.setImageResource(R.drawable.tab_circle_selected);
			circleText.setTextColor(getResources().getColor(R.color.head_bg));
			L.d("==circleFragment="+circleFragment);
			circleFragment = fragmentManager.findFragmentByTag("circleFragment");
			//Toast.makeText(MainActivity.this, circleFragment==null?"null":circleFragment.toString(), Toast.LENGTH_LONG).show();
			if (circleFragment == null) {
				// 如果circleFragment为空，则创建一个并添加到界面上
				circleFragment = new CircleFragment();
				transaction.replace(R.id.content, circleFragment,"circleFragment");
			} else {
				// 如果circleFragment不为空，则直接将它显示出来
				//transaction.replace(R.id.content, circleFragment,"circleFragment");
				transaction.show(circleFragment);
			}
			break;
		case 1:
			// 当点击了联系人tab时，改变控件的图片和文字颜色
			nearbyImage.setImageResource(R.drawable.tab_nearby_selected);
			nearbyText.setTextColor(getResources().getColor(R.color.head_bg));
			Fragment nearbyFragment = fragmentManager.findFragmentByTag("nearbyFragment");
			if (nearbyFragment == null) {
				// 如果nearbyFragment为空，则创建一个并添加到界面上
				nearbyFragment = new NearbyFragment();
				transaction.replace(R.id.content, nearbyFragment,"nearbyFragment");
			} else {
				// 如果nearbyFragment不为空，则直接将它显示出来
				transaction.show(nearbyFragment);
			}
			break;
		case 2:
			// 当点击了动态tab时，改变控件的图片和文字颜色
			helperImage.setImageResource(R.drawable.tab_helper_selected);
			helperText.setTextColor(getResources().getColor(R.color.head_bg));
			Fragment helperFragment = fragmentManager.findFragmentByTag("helperFragment");
			if (helperFragment == null) {
				// 如果helperFragment为空，则创建一个并添加到界面上
				helperFragment = new HelperFragment();
				transaction.replace(R.id.content, helperFragment,"helperFragment");
			} else {
				// 如果helperFragment不为空，则直接将它显示出来
				transaction.show(helperFragment);
			}
			break;
		case 3:
			// 当点击了设置tab时，改变控件的图片和文字颜色
			personalImage.setImageResource(R.drawable.tab_personal_selected);
			personalText.setTextColor(getResources().getColor(R.color.head_bg));
			Fragment personalFragment = fragmentManager.findFragmentByTag("personalFragment");
			if (personalFragment == null) {
				// 如果nearbyFragment为空，则创建一个并添加到界面上
				personalFragment = new PersonalFragment();
				transaction.replace(R.id.content, personalFragment,"personalFragment");
			} else {
				// 如果nearbyFragment不为空，则直接将它显示出来
				transaction.show(personalFragment);
			}
			
			break;
		case 4:
			// 当点击了设置tab时，改变控件的图片和文字颜色
			xiaoxiImage.setImageResource(R.drawable.tab_message_selected);
			xiaoxiText.setTextColor(getResources().getColor(R.color.head_bg));
			Fragment messageFragment = fragmentManager.findFragmentByTag("messageFragment");
			if (messageFragment == null) {
				messageFragment = new MessageFragment();
				transaction.replace(R.id.content, messageFragment,"messageFragment");
			} else {
				// 如果nearbyFragment不为空，则直接将它显示出来
				transaction.show(messageFragment);
			}
			break;
		}
		transaction.commit();
	}

	/**
	 * 清除掉所有的选中状态。
	 */
	private void clearSelection() {
		circleImage.setImageResource(R.drawable.tab_circle_unselected);
		circleText.setTextColor(Color.parseColor("#82858b"));
		nearbyImage.setImageResource(R.drawable.tab_nearby_unselected);
		nearbyText.setTextColor(Color.parseColor("#82858b"));
		helperImage.setImageResource(R.drawable.tab_helper_unselected);
		helperText.setTextColor(Color.parseColor("#82858b"));
		personalImage.setImageResource(R.drawable.tab_personal_unselected);
		personalText.setTextColor(Color.parseColor("#82858b"));
		
		xiaoxiImage.setImageResource(R.drawable.tab_message_unselected);
		xiaoxiText.setTextColor(Color.parseColor("#82858b"));
	}

	/**
	 * 将所有的Fragment都置为隐藏状态。
	 * 
	 * @param transaction
	 *            用于对Fragment执行操作的事务
	 */
	public void hideFragments(FragmentTransaction transaction) {
		Fragment circleMessageFragment = fragmentManager.findFragmentByTag("circleMessageFragment");
		Fragment circleFragment = fragmentManager.findFragmentByTag("circleFragment");
		if (circleMessageFragment != null) {
			transaction.hide(circleMessageFragment);
		}
		if (circleFragment != null) {
			transaction.hide(circleFragment);
		}
		Fragment nearbyFragment = fragmentManager.findFragmentByTag("nearbyFragment");
		Fragment nearbyGroupFragment = fragmentManager.findFragmentByTag("nearbyGroupFragment");
		Fragment nearbyEveryoneFragment = fragmentManager.findFragmentByTag("nearbyEveryoneFragment");
		if (nearbyEveryoneFragment != null) {
			transaction.hide(nearbyEveryoneFragment);
		}
		if (nearbyFragment != null) {
			transaction.hide(nearbyFragment);
		}
		if (nearbyGroupFragment != null) {
			transaction.hide(nearbyGroupFragment);
		}
		if (nearbyFragment != null) {
			transaction.hide(nearbyFragment);
		}
		Fragment helperFragment = fragmentManager.findFragmentByTag("helperFragment");
		if (helperFragment != null) {
			transaction.hide(helperFragment);
		}
		Fragment personalFragment = fragmentManager.findFragmentByTag("personalFragment");
		if (personalFragment != null) {
			transaction.hide(personalFragment);
		}
	}
}
